/**
 * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edgclub.edg.huanxin.activity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

import com.edgclub.edg.Constant;
import com.edgclub.edg.R;
import com.edgclub.edg.activity.GzBaseActivity;
import com.edgclub.edg.utils.util.UserPrefUtils;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.adapter.message.EMAMessage;

public class ContextMenuActivity extends GzBaseActivity {
    public static final int RESULT_CODE_COPY = 1;
    public static final int RESULT_CODE_DELETE = 2;
    public static final int RESULT_CODE_FORWARD = 3;
	public static final int RESULT_CODE_QUITGROUP = 4;

	public boolean groupAdmin = false;

	public UserPrefUtils userPrefUtils;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		userPrefUtils = new UserPrefUtils(this);
		groupAdmin = userPrefUtils.isGroupAdmin();
		EMMessage message = getIntent().getParcelableExtra("message");
		
		int type = message.getType().ordinal();
		if (type == EMMessage.Type.TXT.ordinal()) {
		    if(message.getBooleanAttribute(Constant.MESSAGE_ATTR_IS_VIDEO_CALL, false) ||
		            message.getBooleanAttribute(Constant.MESSAGE_ATTR_IS_VOICE_CALL, false)){
		        setContentView(R.layout.em_context_menu_for_location);
		    }else if(message.getBooleanAttribute(Constant.MESSAGE_ATTR_IS_BIG_EXPRESSION, false)){
		        setContentView(R.layout.em_context_menu_for_image);
				if (groupAdmin && message.getChatType() == EMMessage.ChatType.GroupChat && !message.getFrom().equals(userPrefUtils.getUser().getImid())){
					findViewById(R.id.quitGroup).setVisibility(View.VISIBLE);
					findViewById(R.id.quitGroup_line).setVisibility(View.VISIBLE);
				} else {
					findViewById(R.id.quitGroup).setVisibility(View.GONE);
					findViewById(R.id.quitGroup_line).setVisibility(View.GONE);
				}
		    }else{
		        setContentView(R.layout.em_context_menu_for_text);
				if (groupAdmin && message.getChatType() == EMMessage.ChatType.GroupChat && !message.getFrom().equals(userPrefUtils.getUser().getImid())){
					findViewById(R.id.quitGroup).setVisibility(View.VISIBLE);
					findViewById(R.id.quitGroup_line).setVisibility(View.VISIBLE);
				} else {
					findViewById(R.id.quitGroup).setVisibility(View.GONE);
					findViewById(R.id.quitGroup_line).setVisibility(View.GONE);
				}
		    }
		} else if (type == EMMessage.Type.LOCATION.ordinal()) {
		    setContentView(R.layout.em_context_menu_for_location);
		} else if (type == EMMessage.Type.IMAGE.ordinal()) {
		    setContentView(R.layout.em_context_menu_for_image);
			if (groupAdmin && message.getChatType() == EMMessage.ChatType.GroupChat && !message.getFrom().equals(userPrefUtils.getUser().getImid())){
				findViewById(R.id.quitGroup).setVisibility(View.VISIBLE);
				findViewById(R.id.quitGroup_line).setVisibility(View.VISIBLE);
			} else {
				findViewById(R.id.quitGroup).setVisibility(View.GONE);
				findViewById(R.id.quitGroup_line).setVisibility(View.GONE);
			}
		} else if (type == EMMessage.Type.VOICE.ordinal()) {
		    setContentView(R.layout.em_context_menu_for_voice);
			if (groupAdmin && message.getChatType() == EMMessage.ChatType.GroupChat && !message.getFrom().equals(userPrefUtils.getUser().getImid())){
				findViewById(R.id.quitGroup).setVisibility(View.VISIBLE);
				findViewById(R.id.quitGroup_line).setVisibility(View.VISIBLE);
			} else {
				findViewById(R.id.quitGroup).setVisibility(View.GONE);
				findViewById(R.id.quitGroup_line).setVisibility(View.GONE);
			}
		} else if (type == EMMessage.Type.VIDEO.ordinal()) {
			setContentView(R.layout.em_context_menu_for_video);
		} else if (type == EMMessage.Type.FILE.ordinal()) {
		    setContentView(R.layout.em_context_menu_for_location);
		}
		    
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		finish();
		return true;
	}

	public void copy(View view){
		setResult(RESULT_CODE_COPY);
		finish();
	}
	public void delete(View view){
		setResult(RESULT_CODE_DELETE);
		finish();
	}
	public void forward(View view){
		setResult(RESULT_CODE_FORWARD);
		finish();
	}

	public void quitGroup(View view){
		setResult(RESULT_CODE_QUITGROUP);
		finish();
	}
}
